/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.mapreduce.datatype;

/**
 * A Packable class that encapsulates a long object with creation time.
 */
public class TimedPackableLong implements Packable {
  private static long SIZE = (Long.SIZE / 8) * 2;

  private long value;
  private long time;
  /**
   * Constructs a newly allocated TimedPackableLong obsject that holding a
   * zero value of type long.
   */
  public TimedPackableLong() { this(0L, 0L); }
  /**
   * Constructs a newly allocated TimedPackableLong object that represents
   * the primitive long argument.
   * @param value the value to be represented by the TimedPackableLong.
   * @param time creation time of this TimedPackableLong object.
   */
  public TimedPackableLong(long value, long time) {
    this.value = value;
    this.time = time;
  }
  /**
   * Constructs a newly allocated TimedPackableLong object that represents
   * the primitive long of the PackableLong argument.
   * @param value the value to be represented by the TimedPackableLong.
   * @param time creation time of this TimedPackableLong object.
   */
  public TimedPackableLong(PackableLong value, long time) {
    this(value.get(), time);
  }
  /**
   * Return the long value of this TimedPackableLong object.
   * @return the long value represented by this object
   */
  public long getLong() { return this.value; }
  /**
   * Return the time value of this TimedPackableLong object.
   * @return the time value represented by this object
   */
  public long getTime() { return this.time; }
  /**
   * Indicates whether some other object is "equal to" this one.
   * see {@link Packable#equals}
   */
  @Override
  public boolean equals(Object o) {
    if (o instanceof TimedPackableLong) {
      if (o == this) {
        return true;
      }
      return value == ((TimedPackableLong) o).getLong()
        && time == ((TimedPackableLong) o).getTime();
    }
    else {
      return false;
    }
  }
  /**
   * Returns a hash code value for the object.
   * see {@link Packable#hashCode}
   */
  @Override
  public int hashCode() {
    return (new Long(value)).hashCode();
  }
  /**
   * Indicates which slot this object should be put.
   * see {@link Packable#slot}
   */
  @Override
  public int slot() {
    return this.hashCode();
  }

  @Override
  public long getRoughSize() {
    return SIZE;
  }
}
